﻿using FH.Data;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebSite.Models
{
    public static class ServiceExtensions
    {
        /// <summary>
        /// Adds authentication service
        /// </summary>
        /// <param name="services">Collection of service descriptors</param>
        public static void AddFarmAuthentication(this IServiceCollection services)
        {
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            var authenticationBuilder = services.AddAuthentication(options =>
            {
                options.DefaultScheme = FHCookieAuthenticationDefaults.AuthenticationScheme;
            });

            //add main cookie authentication
            authenticationBuilder.AddCookie(FHCookieAuthenticationDefaults.AuthenticationScheme, options =>
            {
                options.Cookie.Name = FHCookieAuthenticationDefaults.CookiePrefix + FHCookieAuthenticationDefaults.AuthenticationScheme;
                options.Cookie.HttpOnly = true;
                options.LoginPath = FHCookieAuthenticationDefaults.LoginPath;
                options.AccessDeniedPath = FHCookieAuthenticationDefaults.AccessDeniedPath;
                //whether to allow the use of authentication cookies from SSL protected page on the other store pages which are not
                options.Cookie.SecurePolicy = CookieSecurePolicy.None;
                //options.EventsType = typeof(CustomerCookieAuthenticationEvents);
            });
            //add external authentication
            authenticationBuilder.AddCookie(FHCookieAuthenticationDefaults.ExternalProxyAuthentication, options =>
            {
                options.LoginPath = FHCookieAuthenticationDefaults.LoginProxy;
                options.AccessDeniedPath = FHCookieAuthenticationDefaults.AccessDeniedPath;
            });
            authenticationBuilder.AddCookie(FHCookieAuthenticationDefaults.ExternalStoreAuthentication, options =>
            {
                options.LoginPath = FHCookieAuthenticationDefaults.LoginStore;
                options.AccessDeniedPath = FHCookieAuthenticationDefaults.AccessDeniedPath;
            });
            authenticationBuilder.AddCookie(FHCookieAuthenticationDefaults.ExternalAdminAuthentication, options =>
            {
                options.LoginPath = FHCookieAuthenticationDefaults.LoginAdmin;
                options.AccessDeniedPath = FHCookieAuthenticationDefaults.AccessDeniedPath;
            });
        }
    }
}
